//app.js
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  getUserInfo: function (cb) {
    var that = this

    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      wx.getUserInfo({
        success: function (res) {
          that.globalData.userInfo = res.userInfo
          typeof cb == "function" && cb(that.globalData.userInfo)
        },
        fail: function (res) {
          typeof cb == "function" && cb(null)
        }
      })
    }
  },
  checktoken:function(cb){
    var that=this;
    var token = that.globalData.token;
    if (token){
      wx.request({
        url: that.globalData.Domain + '/user/checktoken',
        data: {
          token: token
        },
        method: "POST",
        success: function (res) {
         
          //  console.log(res);
          if (res.data.code == 0) {
            typeof cb == "function" && cb()
          }
          else {
            that.login(cb);
          }
        }
      })
    }
    else
    {
 
      that.login(cb);
    }
  },
  login: function (cb) {
    var that = this;
    wx.login({
      success: function (res) {
        var _code = res.code;
        wx.request({
          url: that.globalData.Domain + '/user/login',
          data: {
            code: _code
          },
          method: "POST",
          success: function (res) {
          //  console.log(res);
            if (res.data.code == 0) {
            
              that.globalData.token = res.data.data.token;
              typeof cb == "function" && cb(res.data.data)
            }
            else if (res.data.code == 10000) {
              that.register(cb);
            }
            else {
              wx.showModal({
                title: '哪有鱼提示',
                content: res.data.msg,
                showCancel: false
              })
            }

          }
        })
      }
    });
  },
  register: function (cb) {
    var that = this;
    wx.login({
      success: function (res) {
        var _code = res.code;
        wx.getUserInfo({
          withCredentials: true,
          success: function (res) {
            wx.request({
              url: that.globalData.Domain + '/user/register',
              data: {
                code: _code,
                encryptedData: res.encryptedData,
                iv: res.iv
              },
              method:"POST",
              success: function (res) {
                if (res.data.code == 0) {
                  that.globalData.token = res.data.data.token;
                  typeof cb == "function" && cb(res.data.data)
                }
                else {
                  wx.showModal({
                    title: '哪有鱼提示',
                    content: res.data.msg,
                    showCancel: false
                  })
                }
              }
            })
          }
        })
      }
    })

  },
  //get locationInfo
  getLocationInfo: function (cb) {
    var that = this;
    if (this.globalData.locationInfo) {
      cb(this.globalData.locationInfo)
    } else {
      wx.getLocation({
        type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
        success: function (res) {
          that.globalData.locationInfo = res;
          cb(that.globalData.locationInfo)
        },
        fail: function () {
          // fail
        },
        complete: function () {
          // complete
        }
      })
    }
  },
  globalData: {
    userInfo: null,
    locationInfo: null,
    Domain: 'http://localhost:65497', //'https://2.xiaobang520.com',//
    SiteDomain: 'http://localhost:55708',
    
    token:''
  }
})